package com.honghh.controller;

import com.honghh.utils.CookieUtils;
import com.honghh.variable.EnvVariable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;

/**
 * description:
 *
 * @author: hh
 * @date: 2018/6/8 10:51
 */
@Controller
public class BaseController {

    @Autowired
    EnvVariable envVariable;

    @RequestMapping("/")
    public String home(HttpServletRequest request, HttpServletResponse response) {

        String username = (String) request.getAttribute("username");
        System.out.println("单点认证username" + username);
        if (StringUtils.isEmpty(username)) {
            return "sso_error";
        } else {
            return "redirect:/index";
        }
    }

    @RequestMapping("/index")
    public String index(HttpServletRequest request, HttpServletResponse response) {

        String username = (String) request.getAttribute("username");

        System.out.println("单点认证username" + username);
        if (StringUtils.isEmpty(username)) {
            return "sso_error";
        } else {
            return "index";
        }
    }

    @RequestMapping("/logout")
    public String logout(HttpServletResponse httpServletResponse) {
        CookieUtils.clear(httpServletResponse, envVariable.getJwtTokenCookieName(), envVariable.getCookieParentDomain());
        return "redirect:/";
    }
}
